package com.weetop.cfw.base.presenter.imp

import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.RegexUtils
import com.weetop.cfw.base.presenter.PersonalInformationPresenter
import com.weetop.cfw.base.view.BaseView
import com.weetop.cfw.base.view.PersonalInformationView
import com.weetop.cfw.bean.ErrorBean
import com.weetop.cfw.bean.PersonalInfoBean
import com.weetop.cfw.bean.UploadUserHeadImgBean
import com.weetop.cfw.callback.RxJavaCallBack
import com.weetop.cfw.event.UpdateUserInfoEvent
import com.weetop.cfw.mine.activity.PersonalInformationActivity
import com.weetop.cfw.utils.EventBusUtils
import com.weetop.cfw.utils.RetrofitUtils
import com.weetop.cfw.utils.RxJavaUtils
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File


class PersonalInformationPresenterImp(var personalInformationView: PersonalInformationView? = null): PersonalInformationPresenter {

    /**
     * 获取个人资料数据
     */
    override fun getPersonalInfoData(personalInformationActivity: PersonalInformationActivity) {
        personalInformationActivity.showLoadingDialog(personalInformationActivity,"正在获取个人信息...")
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().getPersonalInfo(),personalInformationActivity,object:RxJavaCallBack<PersonalInfoBean>(){
            override fun onSuccess(t: PersonalInfoBean?) {
                personalInformationActivity.dismissLoadingDialog()
                personalInformationView?.personalInfoGetSuccess(t)
            }

            override fun onRequestError(t: PersonalInfoBean?) {
                super.onRequestError(t)
                personalInformationActivity.dismissLoadingDialog()
            }

            override fun onErrorResponse(throwable: Throwable?) {
                super.onErrorResponse(throwable)
                personalInformationActivity.dismissLoadingDialog()
            }
        })
    }

    /**
     * 修改用户头像
     */
    override fun uploadUserHeadImg(personalInformationActivity: PersonalInformationActivity, newHeadImgFile: String) {
        personalInformationActivity.showLoadingDialog(personalInformationActivity,"正在上传用户头像...")
        val file = File(newHeadImgFile)
        val image: RequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file)
        val headImgPart = MultipartBody.Part.createFormData("Imgfile", file.name, image)
//        val headImgPart = MultipartBody.Part.createFormData("Imgfile", newHeadImgFile)
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().uploadUserHeadImg(multipartFile = headImgPart),personalInformationActivity,object:RxJavaCallBack<UploadUserHeadImgBean>(){
            override fun onSuccess(t: UploadUserHeadImgBean?) {
                EventBusUtils.post(UpdateUserInfoEvent())
                personalInformationView?.showNativeShortToast("用户头像上传成功")
                personalInformationView?.uploadHeadImgSuccess(t?.face)
                personalInformationActivity.dismissLoadingDialog()
            }

            override fun onRequestError(t: UploadUserHeadImgBean?) {
                super.onRequestError(t)
                personalInformationView?.uploadHeadImgFailed()
             //   personalInformationView?.showNativeShortToast(t?.message)
                personalInformationActivity.dismissLoadingDialog()
            }

            override fun onErrorResponse(throwable: Throwable?) {
                super.onErrorResponse(throwable)
                personalInformationView?.uploadHeadImgFailed()
                personalInformationActivity.dismissLoadingDialog()
            }
        })
    }

    /**
     * 更新用户信息
     */
    override fun updateUserInfo(personalInformationActivity: PersonalInformationActivity, nickName: String, name: String, gender: String,email: String, company: String) {
        if(email.isNotEmpty() && !RegexUtils.isEmail(email)){
            personalInformationView?.showNativeShortToast("请输入合法的邮箱")
        }else if (name.isEmpty()){
            personalInformationView?.showNativeShortToast("请填写真实姓名")
        }else{
            personalInformationActivity.showLoadingDialog(personalInformationActivity,"正在更新用户信息...")
            RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().updateUserInfo(nickName = nickName,gender = gender,name = name,email = email,company = company),personalInformationActivity,object:RxJavaCallBack<ErrorBean>(){
                override fun onSuccess(t: ErrorBean?) {
                    EventBusUtils.post(UpdateUserInfoEvent())
                    personalInformationView?.showNativeShortToast("用户信息更新成功")
                    personalInformationActivity.dismissLoadingDialog()
                    ActivityUtils.finishActivity(personalInformationActivity)
                }

                override fun onRequestError(t: ErrorBean?) {
                    super.onRequestError(t)
                  //  personalInformationView?.showNativeShortToast(t?.message)
                    personalInformationActivity.dismissLoadingDialog()
                }

                override fun onErrorResponse(throwable: Throwable?) {
                    super.onErrorResponse(throwable)
                    personalInformationActivity.dismissLoadingDialog()
                }

            })
        }
    }

    /**
     * 绑定
     */
    override fun attachView(baseView: BaseView) {
        personalInformationView = baseView as? PersonalInformationView
    }

    /**
     * 解绑
     */
    override fun detachView() {
        personalInformationView = null
    }
}